home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / matrixc.zip / 3D.C next >
C/C++ Source or Header  |  1990-05-10  |  4KB  |  233 lines

  1. /* 3d graphics functions - requires matrix.obj */
  2. /* Written by Nigel Salt */
  3. #include <3d.h>
  4. #include <matrix.h>
  5. #include <stdio.h>
  6. #include <math.h>
  7. #include <graph.h>
  8.  
  9. /* global data declarations */
  10. static double p3dat[4]={0.0,0.0,0.0,1.0};
  11. static matrix p3vec={4,1,&p3dat[0]};
  12. static matrixptr pp3vec=&p3vec;
  13.  
  14. static double p3dat2[4]={0.0,0.0,0.0,1.0};
  15. static matrix p3vec2={4,1,&p3dat2[0]};
  16. static matrixptr pp3vec2=&p3vec2;
  17.  
  18.  
  19. static double d2[4][4];
  20. static matrix mat2={4,4,&d2[0][0]};
  21. static matrixptr m2=&mat2;
  22.  
  23. static double d3[4][4];
  24. static matrix mat3={4,4,&d3[0][0]};
  25. static matrixptr m3=&mat3;
  26.  
  27. /* function definitions */
  28. int tran3(m,tx,ty,tz)
  29. matrixptr m;
  30. double tx,ty,tz;
  31. {
  32.   if (m->rows!=4||m->cols!=4)
  33.     {
  34.     fprintf(stderr,"\ntran3 error - matrix must be 4 x 4");
  35.     return 1;
  36.     }
  37.   mid(m);
  38.   *(m->block+0*4+3)=tx;
  39.   *(m->block+1*4+3)=ty;
  40.   *(m->block+2*4+3)=tz;
  41.   return 0;
  42. }
  43.  
  44. int scale3(m,sx,sy,sz)
  45. matrixptr m;
  46. double sx,sy,sz;
  47. {
  48.   if (m->rows!=4||m->cols!=4)
  49.     {
  50.     fprintf(stderr,"\nscale3 error - matrix must be 4 x 4");
  51.     return 1;
  52.     }
  53.   mid(m);
  54.   *(m->block+0*4+0)=sx;
  55.   *(m->block+1*4+1)=sy;
  56.   *(m->block+2*4+2)=sz;
  57. }
  58.  
  59. int rot3(m,theta,axis)
  60. matrixptr m;
  61. double theta;
  62. int axis;
  63. {
  64.   int a1,a2;
  65.   double ct,st;
  66.   if (m->rows!=4||m->cols!=4)
  67.     {
  68.     fprintf(stderr,"\nscale3 error - matrix must be 4 x 4");
  69.     return 1;
  70.     }
  71.   mid(m);
  72.   if (axis>3||axis<0)
  73.     axis=3;
  74.   *(m->block+3*4+4)=1;
  75.   *(m->block+axis*4+axis)=1;
  76.   a1=(axis+1)%3;
  77.   a2=(a1+1)%3;
  78.   ct=cos(theta);
  79.   st=sin(theta);
  80.   *(m->block+a1*4+a1)=ct;
  81.   *(m->block+a2*4+a2)=ct;
  82.   *(m->block+a1*4+a2)=-st;
  83.   *(m->block+a2*4+a1)=st;
  84.   return 0;
  85. }
  86.  
  87. int genrot(px,py,pz,qx,qy,qz,gamma,m)
  88. double px,py,pz;
  89. double qx,qy,qz;
  90. double gamma;
  91. matrixptr m;
  92. {
  93.   double alpha,beta,theta;
  94.   mid(m);
  95.   if (tran3(m2,-px,-py,-pz))
  96.     return 1;
  97.   mmult(m2,m,m3);
  98.   mcopy(m3,m);
  99.   
  100.   theta=angle(qx,qy);
  101.   alpha=theta;
  102.   rot3(m2,-theta,3);
  103.   mmult(m2,m,m3);
  104.   mcopy(m3,m);
  105.   
  106.   theta=angle(qz,sqrt(qx*qx+qy*qy));
  107.   beta=theta;
  108.   rot3(m2,-theta,3);
  109.   mmult(m2,m,m3);
  110.   mcopy(m3,m);
  111.   
  112.   rot3(m2,gamma,3);
  113.   mmult(m2,m,m3);
  114.   mcopy(m3,m);
  115.   
  116.   rot3(m2,beta,2);
  117.   mmult(m2,m,m3);
  118.   mcopy(m3,m);
  119.   
  120.   rot3(m2,alpha,3);
  121.   mmult(m2,m,m3);
  122.   mcopy(m3,m);
  123.   
  124.   tran3(m2,px,py,pz);
  125.   mmult(m2,m,m3);
  126.   mcopy(m3,m);
  127.  
  128.   return 0;
  129. }
  130.  
  131. double angle(ax,ay)
  132. double ax,ay;
  133. {
  134.   double theta;
  135.   if (fabs(ax)>.00001)
  136.     {
  137.     theta=atan(ay/ax);
  138.     if (ax<0.0)
  139.       theta=theta+pi;
  140.     }
  141.   else
  142.     {
  143.     theta=pi/2;
  144.     if (ay<0.0)
  145.       theta=theta+pi;
  146.     if (fabs(ay)<.00001)
  147.       theta=0;
  148.     }
  149.   return theta;
  150. }
  151.  
  152. void p3mult(p,m)
  153. double *p;
  154. matrixptr m;
  155. {
  156.   int i;
  157.   for (i=0;i<3;i++)
  158.     *(pp3vec->block+i)=*(p+i);
  159.   mmult(m,pp3vec,pp3vec2);
  160.   for (i=0;i<3;i++)
  161.     *(p+i)=*(pp3vec2->block+i);
  162. }
  163.  
  164. void objtran(o,m)
  165. objectptr o;
  166. matrixptr m;
  167. {
  168.   int i,j;
  169.   for (i=0;i<o->points;i++)
  170.     p3mult((o->pdat+3*i),m);
  171. }
  172.  
  173. void objprin(o)
  174. objectptr o;
  175. {
  176.   int i,j;
  177.   printf("\nPOINTS");
  178.   for (i=0;i<o->points;i++)
  179.     printf("\n%10.2lf%10.2lf%10.2lf",*(o->pdat+i*3),*(o->pdat+i*3+1),\
  180.            *(o->pdat+i*3+2));
  181.   printf("\nLINES");
  182.   for (i=0;i<o->lines;i++)
  183.     printf("\n%10d%10d",*(o->ldat+i*2),*(o->ldat+i*2+1));
  184. }
  185.  
  186. void objdraw(o)
  187. objectptr o;
  188. {
  189.   int i,j;
  190.   int x,y,pnum;
  191.   for (i=0;i<o->lines;i++)
  192.     {
  193.     pnum=*(o->ldat+i*2);
  194.     x=*(o->pdat+3*pnum);
  195.     y=*(o->pdat+3*pnum+1);
  196.     _moveto(x,y);
  197.     pnum=*(o->ldat+i*2+1);
  198.     x=*(o->pdat+3*pnum);
  199.     y=*(o->pdat+3*pnum+1);
  200.     _lineto(x,y);
  201.     }
  202. }
  203.  
  204. int objcop(s,d)
  205. objectptr s,d;
  206. {
  207.   int i,j;
  208.   if (s->points!=d->points||s->lines!=d->lines)
  209.     {
  210.     fprintf(stderr,"\nobjcop error - objects must be same size");
  211.     return 1;
  212.     }
  213.   for (i=0;i<s->points;i++)
  214.     for (j=0;j<3;j++)
  215.       *(d->pdat+3*i+j)=*(s->pdat+3*i+j);
  216.   for (i=0;i<s->lines;i++)
  217.     for (j=0;j<2;j++)
  218.       *(d->ldat+2*i+j)=*(s->ldat+2*i+j);
  219.   return 0;
  220. }
  221.  
  222. int init3d()
  223. {
  224.   if (!(_setvideomode(_VRES16COLOR)))
  225.     {
  226.     fprintf(stderr,"\ninit3d error - VGA 16 color 640x480 not available");
  227.     return 1;
  228.     }
  229.   _setlogorg(320,240);
  230.   return 0;
  231. }
  232.  
  233.